
const opendoor = '../borrowing/borrowing';//开门等待
const $ = require('../../utils/utils.js')
const user = require('../../manager/userManager.js')
const config = require('../../config.js')
var app = getApp();
var waitingId;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        gridItems: [],
        welcome_img: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/4_2-8.png",
        isNotFristOpen: true,
        errorType:"",
        errorMsg:""
    },

    /******************Life Cycle*************/
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        $.showLoading()
        // this.setData({
        //     gridItems: wx.getStorageSync('gridItems')
        // });

        if (!wx.getStorageSync("isNotFristOpen")) {
            this.setData({
                isNotFristOpen: false
            })
        }
        if (wx.getStorageSync("openErroType")){
            let errorType = wx.getStorageSync("openErroType");
            let errorMsg = wx.getStorageSync("openErrorMsg");
            this.setData({
                errorType: errorType,
                errorMsg: errorMsg 
            })
        }

        this.gridList(options.shelfCode, wx.getStorageSync("isbn_list"))
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // this.setData({
        //     gridItems: wx.getStorageSync('gridItems')
        // });

        let that = this

        wx.removeStorageSync("openErroType")
        wx.removeStorageSync("openErrorMsg")

        if (that.data.errorType == 1){
            wx.showModal({
                title: "友情提示",
                content: that.data.errorMsg,
                cancelText:"继续操作",
                confirmText:"返回首页",
                confirmColor: "#FF3B30",
                success:res=>{
                    if (res.confirm){
                        wx.reLaunch({
                            url: config.page.index,
                        })
                    }
                }
            })
        } else if (that.data.errorType == 2){
            wx.showModal({
                title: "友情提示",
                content: that.data.errorMsg,
                cancelText: "继续操作",
                confirmText: "重新选择",
                confirmColor: "#FF3B30",
                success:res=>{
                    if (res.confirm) {
                        $.goto(config.page.wishList, true)
                    }
                }
            })
        }


    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        if (waitingId) {
            console.warn('清除柜门定时器');
            clearTimeout(waitingId);
        }
        wx.setStorageSync('scanFlag', 0);//设置扫码标识
    },

    // 获取书格列表
    gridList: function (shelf_code, isbn_list=[]) {
        var that = this
        console.log(`#grid_choose_gridList${shelf_code, isbn_list}`)
        //请求当前二维码所在柜子的格子信息
        let data = {
            usertoken: wx.getStorageSync('usertoken'),
            userid: wx.getStorageSync('userid'),
            shelf_code: shelf_code, //,
            isbn_list: isbn_list
        }

        let url = $.getUrl(config.getGridList, data)
        $.get(url).then(data => {
            console.error(data)
            if (data.result == 1) {
                let array = Array.from(data.items)
                let newGridItems = array.map(item => {
                    var gridIndex = item.bookgrid_code.substring(11);
                    item.gridIndex = gridIndex;
                    return item
                })

                that.setData({
                    gridItems: newGridItems
                });
            } else {
                wx.showModal({
                    content: data.message,
                });
            }
        }).catch(error=>{
            console.error(error)
        })
    },


    /*********** Methods***********/

    // 定时跳转到前页
    timeToSkipBack: function () {
        if (waitingId) clearInterval(waitingId);
        waitingId = setTimeout(function () {
            console.warn('定时跳出！');
            $.goto(config.page.index)
        }, 60000 * 5);
    },

    // 开门
    opendoor: function (e) {
        console.log(e);
        var grid_code = e.currentTarget.id;
        wx.setStorageSync('grid_code', grid_code); //grid_code
        var isbn_list = e.currentTarget.dataset.list; //isbn_list
        wx.setStorageSync("grid_wish_list", isbn_list);
        //发起开柜指令
        wx.showLoading({
            title: "正在发送指令...",
            mask: true,
        });

        let params = {
            grid_code: grid_code,
            usertoken: wx.getStorageSync('usertoken'),
            userid: wx.getStorageSync('userid')
        }

        $.post(config.unlock, params).then(data => {
            wx.hideLoading();
            console.log(data);
            console.log(data.code);
            if (data.code == 0) {
                wx.hideToast();
                wx.reLaunch({
                    url: config.page.borrowing,
                })
            } else {
                console.log(data.message);
                wx.showModal({
                    content: data.message,
                    confirmText: "确定",
                    cancelText: "取消",
                });
            }
        })


    },

    // 点击引导页
    tapWelcome: function () {
        wx.setStorageSync("isNotFristOpen", true)
        this.setData({
            isNotFristOpen: true
        })
    },

    //生成formId
    bindSubmit: function (e) {
        console.log(e)
        user.saveMsgFormId(e.detail.formId)
    },
})